package top.myhdg.bijou.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.ArrayList;
import java.util.List;

import top.myhdg.bijou.R;
import top.myhdg.bijou.sqlite.History;
import top.myhdg.bijou.sqlite.HistoryDao;

public class HistoryActivity extends BaseActivity {
    List<History> historyList;
    HistoryDao historyDao;
    ArrayList<HistoryItem> historyItems;
    HistoryAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        final ListView historyListView = findViewById(R.id.historyList);
        historyListView.setVerticalScrollBarEnabled(false);//隐藏普通滚动条
        historyListView.setFastScrollEnabled(false);//隐藏快速滚动条

        historyDao = new HistoryDao(HistoryActivity.this);
        historyList = new ArrayList<>();
        historyItems = new ArrayList<>();
        adapter = new HistoryAdapter(HistoryActivity.this, R.layout.item_history, historyItems);
        historyListView.setAdapter(adapter);
        if (historyDao.isDataExist()) {
            historyList = historyDao.getAllDate();
        }
        historyItems.clear();
        for (History history : historyList) {
            final int itemId = historyList.indexOf(history);
            String title = history.getTitle();
            String website = history.getWebsite();
            String time = history.getTime();
            historyItems.add(new HistoryItem(itemId, title, website, time, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    History historyD = historyList.get(itemId);
                    historyDao.deleteHistory(historyD);
                    historyItems.remove(itemId);
                    historyList.remove(itemId);
                    adapter.notifyDataSetChanged();
                }
            }));
        }
        historyListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("History", historyList.get(position).getWebsite());
                setResult(3, intent);
                HistoryActivity.this.finish();
            }
        });

        ImageButton emptyHistory = findViewById(R.id.emptyHistory);
        emptyHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new QMUIDialog.MessageDialogBuilder(HistoryActivity.this)
                        .setTitle("请确认")
                        .setMessage("清空所有历史记录？")
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction(0, "清空", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                for (int i = 0; i < historyList.size(); i++) {
                                    historyDao.deleteHistory(historyList.get(i));
                                }
                                historyList.clear();
                                historyItems.clear();
                                adapter.notifyDataSetChanged();
                                dialog.dismiss();
                            }
                        })
                        .create().show();
            }
        });

        Button exitHistory = findViewById(R.id.exitHistory);
        exitHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HistoryActivity.this.finish();
            }
        });
    }
}

class HistoryItem {
    private int itemId;
    private String historyTitle;
    private String historyWebsite;
    private String historyTime;
    private View.OnClickListener onClickListener;

    HistoryItem(int itemId, String historyTitle, String historyWebsite, String historyTime, View.OnClickListener onClickListener) {
        this.itemId = itemId;
        this.historyTitle = historyTitle;
        this.historyWebsite = historyWebsite;
        this.historyTime = historyTime;
        this.onClickListener = onClickListener;
    }

    int getItemId() {
        return itemId;
    }

    String getHistoryTitle() {
        return historyTitle;
    }

    String getHistoryWebsite() {
        return historyWebsite;
    }

    String getHistoryTime() {
        return historyTime;
    }

    View.OnClickListener getOnClickListener() {
        return onClickListener;
    }
}

class HistoryAdapter extends ArrayAdapter<HistoryItem> {
    private int resourceId;

    HistoryAdapter(@NonNull Context context, int resourceId, ArrayList<HistoryItem> objects) {
        super(context, resourceId, objects);
        this.resourceId = resourceId;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        HistoryItem historyItem = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.historyTitle = view.findViewById(R.id.historyTitle);
            viewHolder.historyWebsite = view.findViewById(R.id.historyWebsite);
            viewHolder.historyTime = view.findViewById(R.id.historyTime);
            viewHolder.deleteHistory = view.findViewById(R.id.deleteHistory);
            view.setTag(viewHolder); // 将ViewHolder存储在View中
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder
        }
        assert historyItem != null;
        int pageId = historyItem.getItemId() + 1;
        String pageTitle = pageId + ". " + historyItem.getHistoryTitle();
        viewHolder.historyTitle.setText(pageTitle);
        viewHolder.historyWebsite.setText(historyItem.getHistoryWebsite());
        viewHolder.historyTime.setText(historyItem.getHistoryTime());
        viewHolder.deleteHistory.setOnClickListener(historyItem.getOnClickListener());
        return view;
    }

    class ViewHolder {
        TextView historyTitle;
        TextView historyWebsite;
        TextView historyTime;
        ImageButton deleteHistory;
    }
}
